package com.aier.school.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin_new.common.CompresszZipFile;
import com.aier.classes.common.InviteRequestUtil;
import com.aier.classes.entity.ClassParent;
import com.aier.classes.service.IClassUserService;
import com.aier.common.common.Constant;
import com.aier.common.common.ImageUtil;
import com.aier.common.common.SystemConfigStatic;
import com.aier.common.dao.ICommonDAO;
import com.aier.school.entity.DailyFood;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.service.ISchoolService;
import com.aier.school.service.ISchoolTeacherService;
import com.aier.user.service.IUserBuyService;

@Scope("prototype")
@Component("schoolDailyfoodAction")
public class SchoolDailyfoodAction extends SchoolBaseAction{

    /**
     * 学校管理-餐饮管理
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "schoolService")
    private ISchoolService schoolService;    
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;
    @Resource(name = "classUserService")
    private IClassUserService classUserService;
    @Resource(name = "userBuyService")
    private IUserBuyService userBuyService;
    
    private int s;                // 成功:1 ,失败:0
    
    private int page;             // 页数
    private int page_size;        // 每页的记录数
    private int page_count;       // 总页数
    private int count;            // 总数
    
    private Integer sid;          // 学校id
    private Long tid;             // 教师id
    private Integer mesCount;     // 短信条数
    private String dailyfood;     // 餐饮信息（格式：时间,内容@时间,内容）
    private Long dfid;            // 餐饮id
    private String ddate;         // 用餐时间
    private String content;       // 餐饮内容（类型:内容\n类型:内容）
    private Integer tpCount;      // 全校师生的人数，用来统计需要的短信条数
    private File[] image;         // 图片
    private String[] photoName;   // 上传图片的名称或路径
    private Short ismessage;    // 是否群发短信（0否1是）
    private Integer peoCount; // 公告短信发送的人数
    
    
    
    private List<DailyFood> dfList; // 餐饮信息列表
        
    
    @Override
    public String execute(){
        // 查询剩余短信条数
        mesCount = schoolTeacherService.searchSchoolTeacherById(tid).getMesCount();
        List<ClassParent> cpList = classUserService
                .searchClassParentListSid_account(sid);
        List<SchoolTeacher> stList = schoolTeacherService
                .searchSchoolTeacherListSid_account(sid);
        tpCount = cpList.size() + stList.size();
        return SUCCESS;
    }
    
    /**
     * 新增餐饮(s = 10001当日已有餐饮信息)
     * @return
     */
    public String addDailyfood() {
        s = schoolService.insertDailyFood(sid, dailyfood, ismessage);
        return SUCCESS;
    }
    
    /**
     * 删除餐饮
     * @return
     */
    public String deleteDailyfood() {
        s = schoolService.deleteDailyFood(dfid);
        return SUCCESS;
    }
    
    /**
     * 修改餐饮(s=10001当日已有餐饮信息)
     * @return
     */
    public String updateDailyfood() {
        s = schoolService.updateDailyFood(dfid, ddate, content);
        return SUCCESS;
    }
    
    /**
     * 查询餐饮列表
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchDailyfoodList(){
        s = 1;    
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String,Object> map = schoolService.searchDailyFood(page, 
                page_size, sid);
        dfList = (List<DailyFood>) map.get(ICommonDAO.OBJ_LIST);
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;
    }
    
    /**
     * 教师客户端上传餐饮图片
     * @return
     */
    public String uploadDailyfoodImg() {
        String picName = "";
        if (null != image) {
            for (int i = 0; i < image.length; i++) {
                File file = image[i];

                String fileName = ImageUtil.uploadImage(file,
                        SystemConfigStatic.homework_upload_url, file.getName(),
                        (int) 1);

                if (!"".equals(picName)) {
                    picName += ",";
                }
                picName += fileName;
            }
        }
        s = schoolService.insertDailyFoodImg(dfid, picName);
        
        return SUCCESS;

    }
    
    /**
     * app用的方法
     * @return
     */
    public String checkMessageApp(){
    	
        List<SchoolTeacher> stList = new ArrayList<SchoolTeacher>();
        List<ClassParent> cpList = new ArrayList<ClassParent>();
        content = InviteRequestUtil.getFilterMessage(dailyfood);
        if (ismessage==1) {// 全体教师（餐饮）
        	 cpList = classUserService.searchClassParentListSid_account(sid);
             stList = schoolTeacherService.searchSchoolTeacherListSid_account(sid);
        } 
        // 判断公告的长度,计算每条评语需要发送几条短信
        content = content + "【爱儿360】";
        
        int size = 1;
        
        if (content.length() > Constant.num_70) {
            int len = content.length();
            size = ((double)len/(double)Constant.num_67)>(len/Constant.num_67) ? len/Constant.num_67+1 : len/Constant.num_67;
        }
        
        mesCount = (cpList.size() + stList.size())*size;
        peoCount = cpList.size() + stList.size();
        
        School school = userBuyService.searchSchoolMessageCountSid(sid);
        if (school.getMethod() == 2) {
            int c = 0;
                c = schoolTeacherService.searchSchoolTeacherById(tid).getMesCount();
            // 短信条数不足
            if (c < mesCount) {
                s = 0;
            }else{
            	s=1;
            }
        }
        
    	return SUCCESS;
    }
    
    
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getS() {
        return s;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public Integer getSid() {
        return sid;
    }

    public void setDailyfood(String dailyfood) {
        this.dailyfood = dailyfood;
    }

    public void setDfid(Long dfid) {
        this.dfid = dfid;
    }

    public void setDdate(String ddate) {
        this.ddate = ddate;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setClassUserService(IClassUserService classUserService) {
        this.classUserService = classUserService;
    }

    public Integer getTpCount() {
        return tpCount;
    }

    public List<DailyFood> getDfList() {
        return dfList;
    }

    public void setImage(File[] image) {
        this.image = image;
    }

    public void setPhotoName(String[] photoName) {
        this.photoName = photoName;
    }

    public Integer getMesCount() {
        return mesCount;
    }

    public void setIsmessage(Short ismessage) {
        this.ismessage = ismessage;
    }

	public Integer getPeoCount() {
		return peoCount;
	}
}
